<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${companyPackage}.dao.${package}.${beanName}Mapper">
 	<!-- 以下两个<cache>标签二选一,第一个可以输出日志,第二个不输出日志 -->  
    <!--<cache type="org.mybatis.caches.ehcache.LoggingEhcache"/>  -->
   <!-- <cache type="org.mybatis.caches.ehcache.EhcacheCache"/> -->
    <resultMap id="${beanName}ResultMap" type="${companyPackage}.model.${package}.${beanName}">
       <#list fieldList as field>
    		<result property="${field}" column="${field2column[field]}"/>
       </#list>
    </resultMap>

    <sql id="${beanName}_columns">
       	<#list fieldList as field>
       		${field2column[field]}<#if field_index+1< fieldList?size>,</#if>
       	</#list>
    </sql>
    
    <sql id="${beanName}_where_conditions">
    	<trim prefix="where" prefixOverrides="and |or">  
        <#list fieldList as field><if test="${field} != null">
        	${field2column[field]}=#${"{"}${field}${"}"} AND 
        </if></#list>
       </trim>
    </sql>
    
    <sql id="${beanName}_col_pk">
    	<trim suffixOverrides=","><#list pkeys?keys as key> 
        	${pkeys[key]},
        </#list></trim>
    </sql>
    
    <sql id="${beanName}_where_pk">
    	<trim prefix="where" prefixOverrides="and |or">  
        <#list pkeys?keys as key> 
        	${pkeys[key]}=#${"{"}${key}${"}"} AND 
        </#list>
       </trim>
    </sql>
    
    <sql id="${beanName}_field_pk">
    	<trim prefix="where" prefixOverrides="and |or">  
        <#list pkeys?keys as key> 
        	${key},
        </#list>
       </trim>
    </sql>
    
    <sql id="${beanName}_set_conditions">
    	<set>
        <#list fieldList as field>
        <if test="${field} != null">
        	${field2column[field]}=#${"{"}${field}${"}"},
        </if>
        </#list>
        </set>
    </sql>

    <sql id="${beanName}_properties">
        <#list fieldList as field>
        #${"{"}${field2column[field]}${"}"}<#if field_index+1< fieldList?size>,</#if>
        </#list>
    </sql>

    <insert id="create" parameterType="${companyPackage}.model.${package}.${beanName}" keyColumn="id" keyProperty="id" useGeneratedKeys="true" >
        insert INTO ${tableName} (<include refid="${beanName}_columns"/>) VALUES (<include refid="${beanName}_properties"/>)
    </insert>
	
	<insert id="insert" parameterType="${companyPackage}.model.${package}.${beanName}" keyColumn="id" keyProperty="id" useGeneratedKeys="true">
  		INSERT INTO ${tableName}  
  		<trim prefix="(" suffix=")" suffixOverrides="," >
  		<#list fieldList as field>
  			<if test="${field} != null" >
  				${field2column[field]},
  			</if>
  		</#list>
  		</trim>
  		<trim prefix="values (" suffix=")" suffixOverrides="," >
  		<#list fieldList as field>
  			<if test="${field} != null" >
  				${r'#{'}${field}${r'}'},
  			</if>
  		</#list>
  		</trim>
  	</insert>
	<insert id="insertBySql" parameterType="map">
  		 ${r'${sql}'}
  	</insert>
    
    <delete id="deleteByObj" parameterType="${companyPackage}.model.${package}.${beanName}">
        delete from ${tableName} <include refid="${beanName}_where_conditions"/>
    </delete>
    <!--多主键会出现问题-->
  	<delete id="deleteByIdList" parameterType="java.util.List">
    	delete from ${tableName} <include refid="${beanName}_col_pk"/> IN 
    		<foreach item="item" index="index" collection="list" open="(" separator="," close=")">
				${'#{'}item${'}'}
			</foreach>
    </delete>
	<delete id="delete" parameterType="map">
  	      ${r'${sql}'}
  	</delete>
  	
    <update id="updateAllField" parameterType="${companyPackage}.model.${package}.${beanName}">
        update ${tableName} SET
        <#list fieldList as field>
            ${field2column[field]} = #${"{"}${field}${"}"}<#if field_index+1< fieldList?size>,</#if>
        </#list>
        <include refid="${beanName}_where_pk"/>
    </update>
    
    <update id="updateByObj" parameterType="${companyPackage}.model.${package}.${beanName}">
        update ${tableName}  <include refid="${beanName}_set_conditions"/> <include refid="${beanName}_where_pk"/>
    </update>
    
    <update id="update" parameterType="map">
  	     ${r'${sql}'}
  	</update>
  	
    <select id="getOneByObj" resultMap="${beanName}ResultMap" parameterType="${companyPackage}.model.${package}.${beanName}">
        select <include refid="${beanName}_columns"/> from ${tableName} <include refid="${beanName}_where_conditions"/> limit 1
    </select>
	
    <select id="getListAll" resultMap="${beanName}ResultMap" parameterType="${companyPackage}.model.${package}.${beanName}">
        select <include refid="${beanName}_columns"/> from ${tableName} 
    </select>
    
    <select id="getListByObj" resultMap="${beanName}ResultMap"  parameterType="${companyPackage}.model.${package}.${beanName}">
        select <include refid="${beanName}_columns"/> from ${tableName}  <include refid="${beanName}_where_conditions"/>
    </select>
    
    <select id="getListByObjPage" resultMap="${beanName}ResultMap"  parameterType="${companyPackage}.model.${package}.${beanName}">
        select <include refid="${beanName}_columns"/> from ${tableName}  <include refid="${beanName}_where_conditions"/>
    </select>
    
    <select id="getCountByObj" resultType="int" parameterType="${companyPackage}.model.${package}.${beanName}">
        select count(1) from ${tableName} <include refid="${beanName}_where_conditions"/>
    </select>
    
    <select id="selectMap" parameterType="map" resultType="map">
  	      ${r'${sql}'}
  	</select>
  	<select id="select" parameterType="map" resultMap="${beanName}ResultMap">
  	      ${r'${sql}'}
  	</select>
    <select id="selectCount" parameterType="map" resultType="long">
  	      ${r'${sql}'}
  	</select>
</mapper>